package cn.shenlong.portal.controller;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.shenlong.portal.domain.EmailConfig;
import cn.shenlong.portal.service.EmailConfigService;

@Controller
@RequestMapping("/emailConfig/admin")
public class EmailConfigAController {
	
	@Autowired
	private EmailConfigService emailConfigService;

	@RequestMapping("/seedEmailConfig")
	public String seedEmailConfig(Long emailConfigId,ModelMap model){
		if(emailConfigId!=null && emailConfigId>0){
			EmailConfig emailConfig = emailConfigService.findByPK(emailConfigId);
			model.put("emailConfigId", emailConfigId);
			model.put("emailConfig", emailConfig);
			return "/server/emailConfig/show";
		}else{
			model.addAttribute("errorMsg", "参数错误!");
			return "/common/error";
		}
	}
	
	@RequestMapping("/editEmailConfig")
	public String editEmailConfig(Long emailConfigId,ModelMap model){
		if(emailConfigId!=null && emailConfigId>0){
			EmailConfig emailConfig = emailConfigService.findByPK(emailConfigId);
			model.put("emailConfigId", emailConfigId);
			model.put("emailConfig", emailConfig);
			return "/server/emailConfig/edit";
		}else{
			model.addAttribute("errorMsg", "参数错误!");
			return "/common/error";
		}
	}	
	
	@RequestMapping("/updateEmailConfig")
	public String save(EmailConfig emailConfig,ModelMap model ){
		boolean isPass = EmailConfigAController.testMail(emailConfig);
		if (isPass) {
			emailConfigService.updateEntity(emailConfig);
		} else {
			model.put("errMsg", "邮箱测试未通过");
			return editEmailConfig(emailConfig.getEmailConfigId(),model);
		}
		return "redirect:seedEmailConfig.html?emailConfigId=" + emailConfig.getEmailConfigId();
	}
	
	public static boolean testMail(EmailConfig emailConfig) {
		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", emailConfig.getEmailServer());
			props.put("mail.smtp.port", String.valueOf(emailConfig.getEmailPort()));
			props.put("mail.smtp.auth", "true");
			Transport transport = null;
			Session session = Session.getDefaultInstance(props, null);
			transport = session.getTransport("smtp");
			transport.connect(emailConfig.getEmailServer(), emailConfig.getUser(), emailConfig.getPassword());
			MimeMessage msg = new MimeMessage(session);
			msg.setSentDate(new Date());
			InternetAddress fromAddress = new InternetAddress(emailConfig.getEmailFrom());
			msg.setFrom(fromAddress);
			InternetAddress[] toAddress = new InternetAddress[1];
			toAddress[0] = new InternetAddress(emailConfig.getUser());
			msg.setRecipients(Message.RecipientType.TO, toAddress);
			msg.setSubject("test", "UTF-8");
			msg.setText("test", "UTF-8");
			msg.saveChanges();
			transport.sendMessage(msg, msg.getAllRecipients());
			return true;
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
	}
}
